import { Controller, Post, Body, Get } from "@nestjs/common";
import { CategoryService } from "./category.service";
import { CreateCategoryDto } from "./dto/create-category.dto";
import { Category } from "./interfaces/category.interface";
import { Article } from "./interfaces/article.interface";
import { Author } from "./interfaces/author.interface";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";

@Controller()
export class CategoryController {
  constructor(
    private readonly categoryService: CategoryService,
    @InjectModel("Category") private readonly categoryModel: Model<Category>,
    @InjectModel("Article") private readonly articleModel: Model<Article>,
    @InjectModel("Author") private readonly authorModel: Model<Author>,
  ) {}

  @Post("category")
  async create(@Body() createCategoryDto: CreateCategoryDto) {
    return await this.categoryService.create(createCategoryDto);
  }

  @Get("category")
  async findAll(): Promise<Category[]> {
    return this.categoryService.findAll();
  }

  @Post("article")
  async createArticle(@Body() createArticle) {
    return await this.articleModel.create(createArticle);
  }

  @Get("article")
  async findAllArticles() {
    return await this.articleModel.find().populate("category");
  }

  @Post("author")
  async createAuthor(@Body() createAuthor) {
    return await this.authorModel.create(createAuthor);
  }
}
